package com.actions.bluetoothbox2.app;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.actions.bluetoothbox2.R;
import com.actions.bluetoothbox2.util.DButils;
import com.actions.bluetoothbox2.util.SchoolReportAdapter;
import com.actions.bluetoothbox2.util.SchoolReportInfo;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;

public class SchoolReportActivity extends Activity {

    TextView title_back, tv_title;
    private RatingBar ratingbar;
    private TextView tv_his;
    private Button btn_star;
    private ListView listview;
    private List<SchoolReportInfo> list;
    private String data;//当前时间
    private SchoolReportAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_school_report);

        title_back = (TextView) findViewById(R.id.title_back);
        tv_title = (TextView) findViewById(R.id.tv_title);
        ratingbar = (RatingBar) findViewById(R.id.ratingbar);
        btn_star = (Button) findViewById(R.id.btn_star);
        tv_his = (TextView) findViewById(R.id.tv_his);
        listview = (ListView) findViewById(R.id.listview);
        tv_title.setText(getResources().getText(R.string.school_report));

        list = DButils.queryReportAll();


        if (null == list || list.size() == 0) {
            tv_his.setVisibility(View.VISIBLE);
        } else {
            tv_his.setVisibility(View.GONE);
//                Collections.sort(list);
            Collections.reverse(list);//对histories 集合中的数据进行倒叙排序
            adapter = new SchoolReportAdapter(this, list);
            listview.setAdapter(adapter);
        }

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");// HH:mm:ss
//获取当前时间
        Date date = new Date(System.currentTimeMillis());
        data = simpleDateFormat.format(date);


        if (list.size() != 0 && list.get(0).getTime().equals(data)) {
            ratingbar.setRating(list.get(0).star);
        }

        btn_star.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int star = (int) ratingbar.getRating();
                Log.i("TAG", "star==========" + star);
                if (list.size() != 0 && list.get(0).getTime().equals(data)) {
                    SchoolReportInfo reportInfo = list.get(0);

                    reportInfo.setStar(star);
                    DButils.updateReport(reportInfo);
                    adapter.notifyDataSetChanged();
                } else {
                    SchoolReportInfo info = new SchoolReportInfo();
                    info.setStar(star);
                    info.setTime(data);
                    DButils.insertReport(info);
                    list.add(0,info);
                    adapter.notifyDataSetChanged();
                }
            }
        });
        title_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }
}
